package snippets;

import mopex.*;
import java.awt.Color;
import java.lang.reflect.*;

public class UpdatedSetColor {

    static public void main( String[] args ) {
	Rabbit rabbit = new Rabbit();
	setObjectColor( rabbit, Color.WHITE );
	if ( !rabbit.setColorCalled )
	    throw new RuntimeException();
    }

//start extract updatedSetObjectColor
    
    public static void setObjectColor( Object obj, Color color ) {
	Class cls = obj.getClass();                                     
	try {
	    Method method = Mopex.getSupportedMethod( cls, 
						      "setColor",                  
						      new Class[] {Color.class} );
	    method.invoke( obj, new Object[] {color} );                
	}
	catch (NoSuchMethodException ex) {                              
	    throw new IllegalArgumentException(
				 cls.getName() + " does not support"
				 + "method setColor(:Color)");
	}
	catch (IllegalAccessException ex) {                             
	    throw new IllegalArgumentException(
				 "Insufficient access permissions to call"
				 + "setColor(:Color) in class " + cls.getName());
	}
	catch (InvocationTargetException ex) {                          
	    throw new RuntimeException(ex);
	}
    }
//stop extract updatedSetObjectColor

    static public class Rabbit {
	public boolean setColorCalled = false;

	public void setColor( Color c ) { setColorCalled = true; }
    }
}
